﻿using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AviagePortal
{
    class  Helper
    {
        public static string ApproveUsers { get {
            SPWeb web = SPContext.Current.Web;       
            string siteUrl = web.Site.Url;
            string webUrl = web.ServerRelativeUrl;
            StringBuilder sb = new StringBuilder();
            SPSecurity.RunWithElevatedPrivileges(delegate()
               {
                   using (SPSite site = new SPSite(siteUrl))
                   {
                       SPWeb currentWeb = site.OpenWeb(webUrl);
                       SPGroup approveGroup = currentWeb.SiteGroups["AviagePortalApprover"];
                       // approveGroup.Users
                       foreach (SPUser user in approveGroup.Users)
                       {
                           if (!string.IsNullOrWhiteSpace(user.Name))
                           {
                               sb.Append(user.Name + ",");
                           }
                       }

                       if (sb.Length > 0)
                       {
                           sb.Remove(sb.Length - 1, 1);
                       }
                   }
               });

            return sb.ToString();
        } }

        public static string ApproveUsersEmail
        {
            get
            {
                StringBuilder sb = new StringBuilder();
                SPWeb web = SPContext.Current.Web;       
                string siteUrl = web.Site.Url;
                string webUrl = web.ServerRelativeUrl;            
            SPSecurity.RunWithElevatedPrivileges(delegate()
               {
                   using (SPSite site = new SPSite(siteUrl))
                   {
                       SPWeb currentWeb = site.OpenWeb(webUrl);

                       SPGroup approveGroup = currentWeb.SiteGroups["AviagePortalApprover"];
                        foreach (SPUser user in approveGroup.Users)
                        {
                            if (!string.IsNullOrWhiteSpace(user.Email))
                            {
                                sb.Append(user.Email + ",");
                            }
                        }

                        if (sb.Length > 0)
                        {
                            sb.Remove(sb.Length-1, 1);
                        }
                   }
               });

                return sb.ToString();
            } 
        }

        //AviagePortalEditor
        public static string EditorUsers
        {
            get
            {
                StringBuilder sb = new StringBuilder();
                SPWeb web = SPContext.Current.Web;
                string siteUrl = web.Site.Url;
                string webUrl = web.ServerRelativeUrl;
                SPSecurity.RunWithElevatedPrivileges(delegate()
              {
                  using (SPSite site = new SPSite(siteUrl))
                  {
                      SPWeb currentWeb = site.OpenWeb(webUrl);
                      SPGroup approveGroup = currentWeb.SiteGroups["AviagePortalEditor"];
                      foreach (SPUser user in approveGroup.Users)
                      {
                          if (!string.IsNullOrWhiteSpace(user.Name))
                          {
                              sb.Append(user.Name + ",");
                          }
                      }

                      if (sb.Length > 0)
                      {
                          sb.Remove(sb.Length - 1, 1);
                      }
                  }
              });

            return sb.ToString();
        } }

        //AviagePortalEditor
        public static string EditorUsersMail
        {
            get
            {
                StringBuilder sb = new StringBuilder();
                SPWeb web = SPContext.Current.Web;
                string siteUrl = web.Site.Url;
                string webUrl = web.ServerRelativeUrl;
                SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(siteUrl))
                {
                    SPWeb currentWeb = site.OpenWeb(webUrl);
                    SPGroup approveGroup = currentWeb.SiteGroups["AviagePortalEditor"];
                    foreach (SPUser user in approveGroup.Users)
                    {
                        if (!string.IsNullOrWhiteSpace(user.Email))
                        {
                            sb.Append(user.Email + ",");
                        }
                    }

                    if (sb.Length > 0)
                    {
                        sb.Remove(sb.Length - 1, 1);
                    }
                }
            });

                return sb.ToString();
            }
        }

        public static bool SendMail(string to, string cc, string subject, string messageBody)
        {
            bool result = false;
            SPWeb web = SPContext.Current.Web;
            string currentUser = web.CurrentUser.Name;
            int currentUserId = web.CurrentUser.ID;
            string siteUrl = web.Site.Url;
            string webUrl = web.ServerRelativeUrl;
            SPSecurity.RunWithElevatedPrivileges(delegate()
                 {
                     using (SPSite site = new SPSite(siteUrl))
                     {
                         SPWeb currentWeb = site.OpenWeb(webUrl);
                         StringDictionary messageHeaders = new StringDictionary();
                         messageHeaders.Add("content-type", "text/html");
                         messageHeaders.Add("to", to);
                         messageHeaders.Add("cc", cc);
                         messageHeaders.Add("subject", "" + subject);
                         result = SPUtility.SendEmail(currentWeb, messageHeaders, messageBody);
                     }
                 });

            return result;
        }
    }
}
